home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4541 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  53 lines

  1. Path: news.cs.ucla.edu!geoff
  2. From: geoff@ficus.cs.ucla.edu (Geoff Kuenning)
  3. Newsgroups: comp.lang.c++
  4. Subject: Derived classes inter-referencing each other
  5. Date: 30 Jan 1996 21:51:38 GMT
  6. Organization: Ficus Research Project, UCLA Computer Science Department
  7. Message-ID: <4em3ta$2p0@delphi.cs.ucla.edu>
  8. NNTP-Posting-Host: exeter.cs.ucla.edu
  9.  
  10. My problem hasn't gotten quite this bad, so I was able to solve it
  11. by re-ordering declarations.  But I started to wonder how one would
  12. handle the more complex case.
  13.  
  14. Suppose you have a pair of derived classes, foo and bar, derived from
  15. foobase and barbase respectively.  The bases can be declared like
  16. this:
  17.  
  18.     class foobase {
  19.         virtual barbase& givebar();
  20.     };
  21.     class barbase {
  22.         virtual foobase& givefoo();
  23.     };
  24.  
  25. So far, so good.  Now we declare the derived classes.  However, we'd
  26. like givebar() to return the corresponding derived class:
  27.  
  28.     class foo : public foobase {
  29.         virtual bar& givebar();
  30.     };
  31.  
  32. Oops!  Can't do that, because the compiler doesn't yet know that bar
  33. is derived from barbase.  I tried putting a null declaration before foo:
  34.  
  35.     class bar : public barbase;
  36.  
  37. but unsurprisingly the compiler wasn't too happy with this.  The only
  38. solution I can come up with is to declare foo "incorrectly":
  39.  
  40.     class foo : public foobase {
  41.         virtual barbase& givebar();
  42.     };
  43.     class bar : public barbase {
  44.         virtual foo& givefoo();    // This is asymmetric but correct
  45.     };
  46.  
  47. and then use typecasts to get the result of givebar() to be of type bar.
  48.  
  49. Anybody got a better solution?
  50. -- 
  51.     Geoff Kuenning    g.kuenning@ieee.org    geoff@ITcorp.com
  52.     http://www.cs.ucla.edu/ficus-members/geoff/
  53.